package cn.gok.util;

import javafx.scene.media.SubtitleTrack;

import java.security.Key;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @ClassName HashMap例子
 * @Description TODO
 * @Author HP
 * @Date 10:51 2022/7/11
 * @Version 1.0
 **/
public class HashMap例子 {
    public static void main(String[] args) {
//        常用方法();
        遍历();

    }

    private static void 遍历() {
        Map<Integer,String> map = new HashMap<>();
        System.out.println(map.put(1,"aaa"));
        System.out.println(map.put(2,"bbb"));
        System.out.println(map.put(1,"ccc"));
        System.out.println(map.keySet().getClass());

        for(Integer key:map.keySet()){
            System.out.println(key+"--------  -");
        }

        Iterator<Integer> it = map.keySet().stream().iterator();
        while (it.hasNext()){
            Integer key = it.next();
            String value = map.get(key);
            System.out.println(key+"=="+value);
        }

        for (Map.Entry<Integer,String> e:map.entrySet()){
            Integer key = e.getKey();
            String value = e.getValue();
            System.out.println(key+"--"+value);
        }

        Iterator<Map.Entry<Integer,String>> it2 = map.entrySet().iterator();
        while (it2.hasNext()){
            Map.Entry<Integer,String> e = it2.next();
            Integer key = e.getKey();
            String value = e.getValue();
            System.out.println(key+"--"+value);
        }
        Consumer<Integer> con =(k)->{
            System.out.println(k+"===="+map.get(k));
        };
        map.keySet().stream().forEach(con);
        map.keySet().stream().forEach((k)->{
            System.out.println(k+"===="+map.get(k));
        });
        Consumer<Map.Entry<Integer,String>> con2 =(e)->{
            System.out.println(e.getKey()+"===="+e.getValue());
        };
        map.entrySet().stream().forEach(con2);


    }

    private static void 常用方法() {
        Map<Integer,String> map = new HashMap<>();
        System.out.println(map.put(1,"aaa"));
        System.out.println(map.put(2,"bbb"));
        System.out.println(map.put(1,"ccc"));

        System.out.println(map.size());
        System.out.println(map);
        System.out.println(map.remove(2));
        System.out.println(map.remove(100));
        System.out.println(map.containsKey(11));
        System.out.println(map.containsValue("bbb"));
        String value = map.get(1);
    }
}
